package com.jimmyhsu.ecnudaowei.person

import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.animation.DecelerateInterpolator
import com.jimmyhsu.ecnudaowei.Db.User
import com.jimmyhsu.ecnudaowei.Db.UserDbHelper
import com.jimmyhsu.ecnudaowei.MyApplication
import com.jimmyhsu.ecnudaowei.R
import com.jimmyhsu.ecnudaowei.Utils.DownloadUtils
import com.jimmyhsu.ecnudaowei.common.views.RoundDrawable
import com.jimmyhsu.ecnudaowei.coursetable.face.FaceInitActivity
import com.jimmyhsu.ecnudaowei.login.LoginActivity
import com.jimmyhsu.ecnudaowei.verify.SchoolVerifyActivity
import kotlinx.android.synthetic.main.activity_me.*

/**
 * Created by xuyanzhe on 2017/6/6.
 */
class UserActivity: AppCompatActivity() {

    companion object {
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, UserActivity::class.java))
        }
    }

    private val REQUEST_VERIFY: Int = 101
    private val dbHelper: UserDbHelper by lazy { UserDbHelper.getInstance(this) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_me)
        initToolbar()
        initData()
        initEvents()
    }

    private fun showAnimations() {
        val animator = ObjectAnimator.ofInt(card, "translationY",LoginActivity.dp2px(20f, this), 0)
        animator.duration = 400
        animator.interpolator = DecelerateInterpolator()
        animator.start()
    }

    private fun initEvents() {
        personFaceDetect.setOnClickListener { FaceInitActivity.startActivity(this) }
        personEdit.setOnClickListener {
            val intent = Intent(this@UserActivity, ChangeInfoActivity::class.java)
            startActivity(intent)
        }
    }

    private fun initData() {
        val db = dbHelper.readableDatabase
        val cursor = db.query(User.TB_NAME, arrayOf(User.COL_NAME, User.COL_VERIFIED, User.COL_REALNAME),
                null, null, null, null, null)
        cursor.moveToFirst()
        personName.text = cursor.getString(cursor.getColumnIndex(User.COL_NAME))
        val verified = cursor.getInt(cursor.getColumnIndex(User.COL_VERIFIED))
        val realName = cursor.getString(cursor.getColumnIndex(User.COL_REALNAME))
        initVerifyState(verified, realName)
        cursor.close()
    }

    private fun initVerifyState(isVerified: Int, realName: String) {
        Log.e("haha", "isVerified = " + isVerified)
        if (isVerified == 1) {
            personVerify.text = realName
            personVerify.setCompoundDrawables(ContextCompat.getDrawable(this, R.drawable.ic_done_grey600_18dp), null, null, null)
        } else {
            personVerify.text = resources.getString(R.string.go_verify)
            personVerify.setCompoundDrawables(ContextCompat.getDrawable(this, R.drawable.ic_warning_grey600_18dp), null, null, null)
            personVerifyWrapper.setOnClickListener { SchoolVerifyActivity.startActivity(this, REQUEST_VERIFY) }
        }
    }

    override fun onResume() {
        super.onResume()
        loadUserAvatar()
        showAnimations()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_VERIFY && resultCode == RESULT_OK) {
            val name = data!!.getStringExtra(SchoolVerifyActivity.EXTRA_REAL_NAME)
            initVerifyState(1, name)
        }
    }

    private fun loadUserAvatar() {
        val bm = MyApplication.getInstance(this).userHead
        if (bm != null) {
            val drawable = RoundDrawable(bm)
            personHead.setImageDrawable(drawable)
        } else {
            DownloadUtils.downloadImageRound(LoginActivity.BASE_URL + "phpprojects/userimage/"
                    + MyApplication.getInstance(this).name + ".jpg", personHead)
        }
    }

    private fun initToolbar() {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        title = ""
    }

}